#include <QStandardItemModel>
#include <QFileDialog>
#include "util/timeutil.h"
#include "core/randomutil.h"
#include "orderswidget.h"

OrdersWidget::OrdersWidget(QWidget *parent)
: QDockWidget(parent)
{
    this->setFloating(false);
    this->setWindowTitle("Account and Orders");
    this->setFeatures(features() | QDockWidget::DockWidgetVerticalTitleBar);

    m_histTableWidget = new OrdersTableWidget(OrdersTableWidget::ORDERSHIST);
    QStringList strHistList;
    strHistList << "Ticket" << "Symbol" << "Type" << "Lots" << "Open Time" << "Open Price";
    strHistList << "S/L" << "T/P" << "Close Time" << "Close Price"  << "Swap" << "Points" << "Profit";
    m_histTableWidget->init(strHistList);

    m_currTableWidget = new OrdersTableWidget(OrdersTableWidget::ORDERSCURR);
    QStringList strCurrList;
    strCurrList << "Ticket" << "Symbol" << "Type" << "Lots" << "Open Time" << "Open Price";
    strCurrList << "S/L" << "T/P" << "Market Price" << "Comm"  << "Swap" << "Points" << "Profit";
    m_currTableWidget->init(strCurrList);

    m_accountWidget = new AccountWidget();
    m_accountWidget->updateContents();

    m_upDownWidget = new UpDownWidget();

    m_tabWidget = new QTabWidget();
    m_tabWidget->setTabPosition(QTabWidget::South);
    m_tabWidget->addTab(m_histTableWidget, "History Orders");
    m_tabWidget->addTab(m_currTableWidget, "Current Orders");
    m_tabWidget->addTab(m_accountWidget, "Account");
    m_tabWidget->addTab(m_upDownWidget, "Up Down Line");
    m_tabWidget->setCurrentWidget(m_currTableWidget);

    OrderManager *orderManager = OrderManager::inst();
    orderManager->addListener(this);

    m_accountLabel = new QLabel(this);
    this->setWidget(m_tabWidget);
}

void OrdersWidget::onCurrOrdersChanged()
{
    updateCurrOrdersTable();
    m_accountWidget->updateContents();
}

void OrdersWidget::onHistOrdersChanged()
{
    updateHistOrdersTable();
}

void OrdersWidget::updateCurrOrdersTable()
{
    m_currTableWidget->setSortingEnabled(false);
    OrderManager *orderManager = OrderManager::inst();
    m_currTableWidget->clearContents();
    std::vector<NormalOrder*> vecNormOrders = orderManager->getNormOrders();
    std::vector<PendingOrder*> vecPendOrders = orderManager->getPendOrders();
    m_currTableWidget->setRowCount(vecNormOrders.size() + vecPendOrders.size());
    RandomUtil *rUtil = RandomUtil::inst();
    char stime[100];
    for (int i = 0; i < vecNormOrders.size(); ++i) {
        NormalOrder *pOrder = vecNormOrders[i];
        double randomPrice = rUtil->getRandomPrice(pOrder->getOrderSymbol());
        int randomDate = rUtil->getRandomDate(pOrder->getOrderSymbol());
        QTableWidgetItem* item[12];
        item[0] = new QTableWidgetItem(QString::number(pOrder->getOrderNo()));
        item[1] = new QTableWidgetItem(pOrder->getOrderSymbol());
        item[2] = new QTableWidgetItem(pOrder->getOrderTypeStr());
        item[3] = new QTableWidgetItem(QString::number(pOrder->getOrderLots()));
        time_t tmpTime = pOrder->getOrderOpenTime() + randomDate;
        tm *ptm = gmtime(&tmpTime);
        item[4] = new QTableWidgetItem(UTIL::getDateTimeStr(0, ptm));
        item[5] = new QTableWidgetItem(QString::number(pOrder->getOrderOpenPrice() + randomPrice));
        if (pOrder->getOrderStopLoss() == 0.0) {
            item[6] = new QTableWidgetItem(QString::number(0));
        }
        else {
            item[6] = new QTableWidgetItem(QString::number(pOrder->getOrderStopLoss() + randomPrice));
        }
        if (pOrder->getOrderTakeProfit() == 0.0) {
            item[7] = new QTableWidgetItem(QString::number(0));
        }
        else {
            item[7] = new QTableWidgetItem(QString::number(pOrder->getOrderTakeProfit() + randomPrice));
        }
        item[8] = new QTableWidgetItem(QString::number(pOrder->getOrderCurrPrice() + randomPrice));
        item[9] = new QTableWidgetItem("");
        item[10] = new QTableWidgetItem("");
        item[11] = new QTableWidgetItem(QString::number(pOrder->getOrderPoint()));
        item[12] = new QTableWidgetItem(QString::number(pOrder->getOrderNetProfit()));
        for (int tmpIndex = 0; tmpIndex <= 12; ++tmpIndex) {
            item[tmpIndex]->setTextAlignment(Qt::AlignHCenter);
            m_currTableWidget->setItem(i, tmpIndex, item[tmpIndex]);
        }
    }

    for (int i = 0; i < vecPendOrders.size(); ++i) {
        PendingOrder *pOrder = vecPendOrders[i];
        double randomPrice = rUtil->getRandomPrice(pOrder->getOrderSymbol());
        int randomDate = rUtil->getRandomDate(pOrder->getOrderSymbol());
        QTableWidgetItem* item[12];
        item[0] = new QTableWidgetItem(QString::number(pOrder->getOrderNo()));
        item[1] = new QTableWidgetItem(pOrder->getOrderSymbol());
        item[2] = new QTableWidgetItem(pOrder->getOrderTypeStr());
        item[3] = new QTableWidgetItem(QString::number(pOrder->getOrderLots()));
        time_t tmpTime = pOrder->getOrderOpenTime() + randomDate;
        tm *ptm = gmtime(&tmpTime);
        item[4] = new QTableWidgetItem(UTIL::getDateTimeStr(0, ptm));
        item[5] = new QTableWidgetItem(QString::number(pOrder->getOrderOpenPrice() + randomPrice));
        if (pOrder->getOrderStopLoss() == 0.0) {
            item[6] = new QTableWidgetItem(QString::number(0));
        }
        else {
            item[6] = new QTableWidgetItem(QString::number(pOrder->getOrderStopLoss() + randomPrice));
        }
        if (pOrder->getOrderTakeProfit() == 0.0) {
            item[7] = new QTableWidgetItem(QString::number(0));
        }
        else {
            item[7] = new QTableWidgetItem(QString::number(pOrder->getOrderTakeProfit() + randomPrice));
        }
        item[8] = new QTableWidgetItem(QString::number(pOrder->getOrderCurrPrice() + randomPrice));
        item[9] = new QTableWidgetItem("");
        item[10] = new QTableWidgetItem("");
        item[11] = new QTableWidgetItem("");
        item[12] = new QTableWidgetItem("");
        for (int tmpIndex = 0; tmpIndex <= 12; ++tmpIndex) {
            item[tmpIndex]->setTextAlignment(Qt::AlignHCenter);
            m_currTableWidget->setItem(vecNormOrders.size() + i, tmpIndex, item[tmpIndex]);
        }
    }

    m_currTableWidget->setSortingEnabled(true);
    m_currTableWidget->update();
}

void OrdersWidget::updateHistOrdersTable()
{
    m_histTableWidget->setSortingEnabled(false);
    OrderManager *orderManager = OrderManager::inst();
    std::vector<HistoryOrder*> vecOrders = orderManager->getHistOrders();
    m_histTableWidget->clearContents();
    m_histTableWidget->setRowCount(vecOrders.size());
    RandomUtil *rUtil = RandomUtil::inst();
    char stime[100];
    for (int i = 0; i < vecOrders.size(); ++i) {
        HistoryOrder *pOrder = vecOrders[i];
        double randomPrice = rUtil->getRandomPrice(pOrder->getOrderSymbol());
        int randomDate = rUtil->getRandomDate(pOrder->getOrderSymbol());
        QTableWidgetItem* item[12];
        item[0] = new QTableWidgetItem(QString::number(vecOrders[i]->getOrderNo()));
        item[1] = new QTableWidgetItem(vecOrders[i]->getOrderSymbol());
        item[2] = new QTableWidgetItem(pOrder->getOrderTypeStr());
        item[3] = new QTableWidgetItem(QString::number(vecOrders[i]->getOrderLots()));
        time_t tmpStartTime = pOrder->getOrderOpenTime() + randomDate;
        tm *ptm = gmtime(&tmpStartTime);
        item[4] = new QTableWidgetItem(UTIL::getDateTimeStr(0, ptm));
        item[5] = new QTableWidgetItem(QString::number(vecOrders[i]->getOrderOpenPrice() + randomPrice));
        if (vecOrders[i]->getOrderStopLoss() == 0.0) {
            item[6] = new QTableWidgetItem(QString::number(0));
        }
        else {
            item[6] = new QTableWidgetItem(QString::number(vecOrders[i]->getOrderStopLoss() + randomPrice));
        }
        if (vecOrders[i]->getOrderTakeProfit() == 0.0) {
            item[7] = new QTableWidgetItem(QString::number(0));
        }
        else {
            item[7] = new QTableWidgetItem(QString::number(vecOrders[i]->getOrderTakeProfit() + randomPrice));
        }
        time_t tmpEndTime = pOrder->getOrderCloseTime() + randomDate;
        ptm = gmtime(&tmpEndTime);
        item[8] = new QTableWidgetItem(UTIL::getDateTimeStr(0, ptm));
        item[9] = new QTableWidgetItem(QString::number(vecOrders[i]->getOrderClosePrice() + randomPrice));
        item[10] = new QTableWidgetItem("");
        item[11] = new QTableWidgetItem(QString::number(vecOrders[i]->getOrderPoint()));
        item[12] = new QTableWidgetItem(QString::number(vecOrders[i]->getOrderNetProfit()));
        for (int tmpIndex = 0; tmpIndex <= 12; ++tmpIndex) {
            item[tmpIndex]->setTextAlignment(Qt::AlignHCenter);
            m_histTableWidget->setItem(i, tmpIndex, item[tmpIndex]);
        }
    }
    m_histTableWidget->setSortingEnabled(true);
    m_histTableWidget->update();
}